package com.yc.controller.system.medical;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yc.controller.base.BaseController;
import com.yc.entity.Page;
import com.yc.entity.system.User;
import com.yc.service.system.medical.IMedicalService;
import com.yc.util.Const;
import com.yc.util.Jurisdiction;
import com.yc.util.PageData;



@Controller
@RequestMapping(value="/medical")
public class MedicalController extends BaseController {
	
	String menuUrl = "medical/getlist.do"; //菜单地址(权限用)
	
	@Resource
	private IMedicalService medicalService;
	
	@RequestMapping(value="/getlist")
	public ModelAndView getHospitalList(Page page,HttpSession session)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String keywords = pd.getString("keywords");				//关键词检索条件
		if(null != keywords && !"".equals(keywords)){
			pd.put("keywords", keywords.trim());
		}
		
		User user = (User) session.getAttribute(Const.SESSION_USER);
		pd.put("PUBLISHER", user.getUSER_ID());
		
		page.setPd(pd);
		List<PageData> list = medicalService.getMedicalList(page);
		

		mv.setViewName("system/medica/medica_list");
		mv.addObject("medicaList", list);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
		
	}
	
	
	@RequestMapping(value="/goAddMedica")
	public ModelAndView goAddMedica() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();

		mv.setViewName("system/medica/medica_edit");
		mv.addObject("action", "saveMedica");

		return mv;
		
	}
	
	
	@RequestMapping(value="/saveMedica")
	public ModelAndView saveMedica(HttpSession session) throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"新增Medica");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		User user = (User) session.getAttribute(Const.SESSION_USER);
		pd.put("PUBLISHER", user.getUSER_ID());
		pd.put("MEDICA_ID", this.get32UUID());	//主键
		pd.put("RCDSTS", "A");
		
		medicalService.save(pd);
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	
	@RequestMapping("/goEditMedica")
	public ModelAndView goEditMedica() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData item = new PageData();
		item = this.getPageData();
	
	
		PageData pd = medicalService.findById(item);
		
		mv.setViewName("system/medica/medica_edit");
		mv.addObject("item", item);
		mv.addObject("action", "editMedica");
		mv.addObject("pd", pd);
		
		return mv;
		
	}
	
	@RequestMapping("/editMedica")
	public ModelAndView editMedica() throws Exception{
		
		logBefore(logger, Jurisdiction.getUsername()+"修改Medica");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
	
		pd.put("RCDSTS", "A");
		
		medicalService.edit(pd);
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
		
	}
	
	
	/**删除
	 * @param out
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out) throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"删除Medica");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限
		PageData pd = new PageData();
		pd = this.getPageData();
		medicalService.delete(pd);
		out.write("success");
		out.close();
	}
	
	/**查看
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/view")
	public ModelAndView view() throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();


		pd = medicalService.findById(pd);						//根据ID读取
		
		mv.setViewName("system/medica/medica_view");
		mv.addObject("msg", "editMedica");
		mv.addObject("pd", pd);
		return mv;
	}

}
